﻿using System;
using System.Windows;
using System.Windows.Forms;
using FileSecurityManager.BizEntities;
using FileSecurityManager.BizLogic;
using Application = System.Windows.Application;

namespace FileSecurityManager.UI.UserControls
{
    /// <summary>
    /// Interaction logic for UserOptionsCtl.xaml
    /// </summary>
    public partial class UserOptionsCtl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UserOptionsCtl"/> class.
        /// </summary>
        public UserOptionsCtl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the btnBrowse control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            DialogResult result = dialog.ShowDialog();
            if (result == DialogResult.OK)
            {
                tbWorkingDirectory.Text = dialog.SelectedPath;
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            tbWorkingDirectory.Text = string.Empty;
            if (LoginLogic.CurrentUser.UserOptions != null)
            {
                tbWorkingDirectory.Text = LoginLogic.CurrentUser.UserOptions.WorkingDir;
                cbIsExcel.IsChecked = LoginLogic.CurrentUser.UserOptions.IsExcel;
                cbIsImages.IsChecked = LoginLogic.CurrentUser.UserOptions.IsImages;
                cbIsMusic.IsChecked = LoginLogic.CurrentUser.UserOptions.IsMusic;
                cbIsPDF.IsChecked = LoginLogic.CurrentUser.UserOptions.IsPDF;
                cbIsVideo.IsChecked = LoginLogic.CurrentUser.UserOptions.IsVideo;
                cbIsWord.IsChecked = LoginLogic.CurrentUser.UserOptions.IsWord;
            }

            if (LoginLogic.CurrentUser.UserRole.RoleName == "SuperAdmin")
            {
                ddlUserRole.Items.Clear();
                ddlUserRole.Items.Add("System Administrator");
                ddlUserRole.Items.Add("Administrator");
                ddlUserRole.Items.Add("User");
            }
            else if (LoginLogic.CurrentUser.UserRole.RoleName == "Admin")
            {
                ddlUserRole.Items.Clear();
                ddlUserRole.Items.Add("Administrator");
                ddlUserRole.Items.Add("User");
            }
            else
            {
                ddlUserRole.Items.Clear();
                ddlUserRole.Items.Add("User");
            }

            ddlUserRole.SelectedIndex = 0;
        }

        private void btnExit_OnClick(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }

        /// <summary>
        /// Handles the Click event of the btnSaveChanges control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            UsersLogic logic = new UsersLogic();
            if (LoginLogic.CurrentUser.UserOptions == null)
            {
                LoginLogic.CurrentUser.UserOptions = new UserOptionsModel();
            }

            LoginLogic.CurrentUser.UserOptions.WorkingDir = tbWorkingDirectory.Text;
            LoginLogic.CurrentUser.UserOptions.IsExcel = cbIsExcel.IsChecked ?? false;
            LoginLogic.CurrentUser.UserOptions.IsImages = cbIsImages.IsChecked ?? false;
            LoginLogic.CurrentUser.UserOptions.IsMusic = cbIsMusic.IsChecked ?? false;
            LoginLogic.CurrentUser.UserOptions.IsPDF = cbIsPDF.IsChecked ?? false;
            LoginLogic.CurrentUser.UserOptions.IsVideo = cbIsVideo.IsChecked ?? false;
            LoginLogic.CurrentUser.UserOptions.IsWord = cbIsWord.IsChecked ?? false;

            logic.UpdateUser(LoginLogic.CurrentUser);
        }
    }
}
